//: Playground - noun: a place where people can play

import UIKit

// 闭包
let studname = {print("clusure")};
studname();

let sum = {(no1: Int, no2: Int) -> Int in
    return no1 + no2
}
sum(12, 23)

func sum(no1: Int, no2: Int) -> Int {
    return no1 + no2
}
sum(no1:12, no2:23)

let divide = {(val1: Int, val2: Int) -> Int in
    return val1/val2;
}
divide(3000, 12);

let names = ["AT", "AE", "D", "S", "BE"]
func backwards(s1: String, s2: String) -> Bool {
    return s1 > s2
}
var reversed = names.sorted(by: backwards)
reversed = names.sorted(by: { (str1: String, str2: String) -> Bool in
    return str1<str2;
})
reversed;


func printIfTrue(_ predicate: () -> Bool) {
    if predicate() {
        print("===result is true===")
    }
}

printIfTrue { () -> Bool in
    return true
}

printIfTrue({return 2 > 3})
// 使用尾随闭包的方式调用
printIfTrue() {
    return 5 > 2
}
printIfTrue{
    return 4 > 2
}
printIfTrue{ 3 > 2 }

// @autoclosure修饰的闭包
func printIfTrue2( _ predicate: @autoclosure () -> Bool, predicate2:  @autoclosure () -> Bool) {
    if predicate() && predicate2() {
        print("===22 result is true===")
    }
}

// 使用自动闭包
printIfTrue2(2>1, predicate2: 3>2)



